#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   APP_TesterPresent_0x3E_test.py
@Time    :   2025/04/07 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangl@genvict.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

from pytest_assume.plugin import assume
import Set_up 
import pytest
import time
import requests
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable
sys.dont_write_bytecode = True

TesterPresent = [
    ('3E 00', '7E 00'),   
    ('3E 80', ''),         
]

# 定义NRC测试用例
TesterPresent_NRC13 = [
    ('3E 80 01', '7F 3E 13'),   # 长度不符
    ('3E', '7F 3E 13'),         # 缺少子功能
    ('3E 00 01', '7F 3E 13'),   
    ('3E 80 00', '7F 3E 13')  
]

TesterPresent_NRC12 = [
    ('3E 01', '7F 3E 12'),      # 不支持子功能
    ('3E FF', '7F 3E 12'),      # 无效子功能
    ('3E 02', '7F 3E 12'),      # 长度不符
    ('3E 83', '7F 3E 12'),
]

class Test_APP_TesterPresent_Phy(object):
    ids = [f'Request {req}' for req,resp in TesterPresent]
    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-3E服务正响应')
    @allure.description('测试物理寻址默认会话下3E服务正响应')
    @pytest.mark.parametrize('req,resp', TesterPresent, ids=ids)
    def test_TesterPresent_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    ids = [f'Request {req}' for req,resp in TesterPresent]
    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-3E服务正响应')
    @allure.description('测试物理寻址扩展会话下3E服务正响应')
    @pytest.mark.parametrize('req,resp', TesterPresent, ids=ids)
    def test_TesterPresent_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC13]
    @allure.severity('minor')
    @allure.title('物理寻址-默认会话-3E服务NRC13响应')
    @allure.description('测试物理寻址默认会话下3E服务NRC13响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC13]
    @allure.severity('minor')
    @allure.title('物理寻址-扩展会话-3E服务NRC13响应')
    @allure.description('测试物理寻址扩展会话下3E服务NRC13响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC12]
    @allure.severity('minor')
    @allure.title('物理寻址-默认会话-3E服务NRC12响应')
    @allure.description('测试物理寻址默认会话下3E服务NRC12响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC12]
    @allure.severity('minor')
    @allure.title('物理寻址-扩展会话-3E服务NRC12响应')
    @allure.description('测试物理寻址扩展会话下3E服务NRC12响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址-默认会话-3E服务-NRC12 13优先级')
    @allure.description('测试物理寻址默认会话下3E服务NRC12 13优先级')
    def test_TesterPresent_NRC12andNRC13_Priority_Default_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 01 02', '7F 3E 12', 1, iStep, False)

    @allure.severity('normal')
    @allure.title('物理寻址-扩展会话-3E服务-NRC12 13优先级')
    @allure.description('测试物理寻址扩展会话下3E服务NRC12 13优先级')
    def test_TesterPresent_NRC12andNRC13_Priority_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 81 02', '7F 3E 12', 3, iStep, False)

class Test_APP_TesterPresent_Func(object):
    ids = [f'Request {req}' for req,resp in TesterPresent]
    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-3E服务正响应')
    @allure.description('测试功能寻址默认会话下3E服务正响应')
    @pytest.mark.parametrize('req,resp', TesterPresent, ids=ids)
    def test_TesterPresent_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    ids = [f'Request {req}' for req,resp in TesterPresent]
    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-3E服务正响应')
    @allure.description('测试功能寻址扩展会话下3E服务正响应')
    @pytest.mark.parametrize('req,resp', TesterPresent, ids=ids)
    def test_TesterPresent_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC13]
    @allure.severity('minor')
    @allure.title('功能寻址-默认会话-3E服务NRC13响应')
    @allure.description('测试功能寻址默认会话下3E服务NRC13响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun', 'noClearFault')

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC13]
    @allure.severity('minor')
    @allure.title('功能寻址-扩展会话-3E服务NRC13响应')
    @allure.description('测试功能寻址扩展会话下3E服务NRC13响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC13, ids=ids)
    def test_TesterPresent_NRC13_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun', 'noClearFault')

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC12]
    @allure.severity('minor')
    @allure.title('功能寻址-默认会话-3E服务NRC12响应')
    @allure.description('测试功能寻址默认会话下3E服务NRC12响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_Default_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 1, iStep, False, 'UDSFun', 'noClearFault')

    ids = [f'Request {req}' for req,resp in TesterPresent_NRC12]
    @allure.severity('minor')
    @allure.title('功能寻址-扩展会话-3E服务NRC12响应')
    @allure.description('测试功能寻址扩展会话下3E服务NRC12响应')
    @pytest.mark.parametrize('req,resp', TesterPresent_NRC12, ids=ids)
    def test_TesterPresent_NRC12_Extended_Func(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '', 3, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-默认会话-3E服务-NRC12 13优先级')
    @allure.description('测试功能寻址默认会话下3E服务NRC12 13优先级')
    def test_TesterPresent_NRC12andNRC13_Priority_Default_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 03 02', '', 1, iStep, False, 'UDSFun', 'noClearFault')

    @allure.severity('normal')
    @allure.title('功能寻址-扩展会话-3E服务-NRC12 13优先级')
    @allure.description('测试功能寻址扩展会话下3E服务NRC12 13优先级')
    def test_TesterPresent_NRC12andNRC13_Priority_Extended_Func(self, action, Driver):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 83 02', '', 3, iStep, False, 'UDSFun', 'noClearFault')